<template>
    <div>
      <el-row>
          <el-col :span="16" style="text-align: left;">
              <el-form :inline="true" :model="queryApplicationForm" class="demo-form-inline">
                  <el-form-item label="仓库">
                      <el-select v-model="queryApplicationForm.warehouseId" placeholder="请选择仓库">
                          <el-option
                          v-for="item in warehouses"
                          :key="item.warehouseId"
                          :label="item.warehouseName"
                          :value="item.warehouseId">
                          </el-option>
                      </el-select>
                  </el-form-item>
                  <!-- <el-form-item label="审核状态">
                      <el-input v-model="queryApplicationForm.checkType" placeholder="请选择审核状态"></el-input>
                  </el-form-item>
                  &nbsp;&nbsp; -->
                  <el-button type="primary" @click="query">查询</el-button>
              </el-form>
          </el-col>
          <el-col :span="6" :offset="2" style="text-align: right;">
              <el-button-group>
                  <el-button type="primary" @click="addApplication">新增</el-button>
                  <el-button type="primary" @click="delApplication">删除</el-button>
              </el-button-group>
              &nbsp;&nbsp;
              <el-button type="primary" @click="viewProcess">查看流程图</el-button>
          </el-col>
      </el-row>
  
      <el-row>
          <el-col :span="22" :offset="1">
              <el-table
                  ref="multipleTable"
                  height="600px"
                  :data="tableData"
                  tooltip-effect="dark"
                  style="width: 100%;"
                  stripe
                  border
                  highlight-current-row
                  @selection-change="handleSelectionChange">
                  <el-table-column
                  type="selection"
                  align="center"
                  width="55">
                  </el-table-column>
                  <el-table-column
                  prop="applicationId"
                  label="申请编号"
                  align="center"
                  width="80">
                  </el-table-column>
                  <el-table-column
                  prop="applicationName"
                  label="申请类型"
                  align="center"
                  width="80">
                  </el-table-column>
                  <el-table-column
                  prop="applicationEName"
                  label="申请人"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  prop="dname"
                  label="申请部门"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  label="申请仓库"
                  align="center">
                  <template slot-scope="scope">{{ warehouses[scope.row.warehouseId - 1].warehouseName }}</template>
                  </el-table-column>
                  <el-table-column
                  prop="comment"
                  label="申请原因"
                  align="left">
                  </el-table-column>
                  
                  <el-table-column
                  prop="applicationTime"
                  label="申请时间"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  prop="auditEName"
                  label="审核人"
                  align="center">
                  </el-table-column>
                  <el-table-column 
                  prop="checkTime"
                  label="审核时间"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  label="审核状态"
                  align="center">
                  <template slot-scope="scope"> 
                    <span v-if="scope.row.checkType==1">{{scope.row.checkTypeDESC}}</span>
                    <span v-if="scope.row.checkType==2">仓管员审核</span>
                    <span v-if="scope.row.checkType==3 || scope.row.checkType==4">审核结束</span>
                  </template>
                  </el-table-column>
                  
                  <el-table-column label="操作" align="center">
                      <template slot-scope="scope">
                          <el-button
                          size="mini"
                          @click="rowEditApplication(scope.$index, scope.row)" v-if="scope.row.checkType == 1">修改申请</el-button>
                          <el-button
                          size="mini"
                          @click="viewApplication(scope.$index, scope.row)" v-if="scope.row.checkType != 1">查看申请</el-button>
                      </template>
                  </el-table-column>
              </el-table>
              <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :page-sizes="[10, 15, 20]"
              :page-size="100"
              layout=" prev, pager, next, total, sizes,jumper"
              :total="total">
              </el-pagination>
          </el-col>
      </el-row>
      <ReturnProcess :isShow="dialogVisible" :appplicationCheckType="proseccCheckType" @closeDialog="closeDialog"></ReturnProcess>
    </div>
  </template>
  
  <script>
  import ReturnProcess from '@/components/ReturnProcess.vue';
  export default {
      name: 'GoodsLogManager',
      components: {
        ReturnProcess
    },
      data(){
          return{
              queryApplicationForm: {
                  warehouseId:"",
                  checkType:"",
                  applicationEmpNo:0,
                  applicationEName:'',
                  applicationName:"归还申请",
                  current:1,
                  size:10
              },
              total:0,
              tableData: [],
              warehouses:[],
              checkedData:[],
              readProcessId:0,
            proseccCheckType:0,
            dialogVisible:false
          }
      },
      methods:{
          query(){
              this.$http.post("/lc/getApplicationWithQueryApplicationDTO",this.queryApplicationForm).then(res => {
                  this.tableData = res.data.data;
                  this.total = res.data.total;
              }).catch(err => {
                console.error("Error querying applications:", err);
                this.$message.error("查询申请单失败: " + err.message);
              })
          },

          viewProcess(){
            if (this.checkedData.length != 1){
                this.$message.error("请选择一条数据")
                return
            }
            this.proseccCheckType = this.checkedData[0].checkType
            this.dialogVisible = true
        },
        closeDialog(){
            this.dialogVisible = false
            this.proseccCheckType = 0
        },
          
          getWarehouse(){
              this.$http.get("/lc/queryWarehouse").then(res => {
                  this.warehouses = res.data;
              }).catch(err => {
                console.error("Error getting warehouses:", err);
                this.$message.error("获取仓库信息失败: " + err.message);
              })
          },
          handleSizeChange(val) {
               this.queryApplicationForm.size = val;
          },
          handleCurrentChange(val) { 
              this.queryApplicationForm.current = val;
          },
          delApplication(){
            let pass = true
            let delIds = []
            for (let application of this.checkedData){
                delIds.push(application.applicationId)
                if (application.checkType != 1 && application.checkType != 4){
                    pass = false
                }
            }
            if (!pass){
                this.$message.error("只能删除未提交或者审核完成的申请单！")
                return
            }
            // console.log(delIds)
            if (pass){
                this.$confirm('此操作将删除所选申请单, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    
                    this.$http.post("/lc/deleteApplication",delIds).then(res => {
                        if (res.data == 'success'){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.query()
                        } else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                    }).catch(err => {
                      console.error("Error deleting application:", err);
                      this.$message.error("删除申请单失败: " + err.message);
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                })
            }
        },
          handleSelectionChange(val) { 
              this.checkedData = val;
          },
          viewApplication(index, row) { 
              
            row.dName = row.dname
            this.$router.push({
                path:"/viewRApplication",
                query:{
                    applicationInfo:row,
                    // applicationGoods:res.data,
                    warehouses:this.warehouses,
                    readonly:true,
                //   fromPath:this.$route.path
                }
            })
             
          },
          
          addApplication() { 
              this.$router.push({
                  path: '/addorEditRApplication',
                  query: {
                      applicationName:"归还申请",
                      warehouses:this.warehouses,

                  }
              })
  
          },
          rowEditApplication(index, row) { 
              // console.log(row)
              this.$http.get("/lc/getApplicationGoods?applicationId="+row.applicationId).then(res => {
                  // console.log(res.data)
                //   console.log(row)
                  for (let goods of res.data){
                      goods.warehouseName = this.warehouses[row.warehouseId-1].warehouseName
                  }
                  row.dName = row.dname
                  this.$router.push({
                      path:"/addorEditRApplication",
                      query:{
                          warehouses:this.warehouses,
                          applicationInfo:row,
                          applicationGoods:res.data,
                          
                      }
                  })
              }).catch(err => {
                console.error("Error getting application goods:", err);
                this.$message.error("获取申请物资信息失败: " + err.message);
              })
              
          },
      },
      mounted() { 
          this.queryApplicationForm.applicationEmpNo = localStorage.getItem("empno")
          this.getWarehouse()
          this.query()
          if (this.$route.query.msg != null){
              this.$message.success(this.$route.query.msg)
          }
          
      },
  
  }
  </script>
  
  <style>
  
  </style>